<?php

namespace backend\controllers;

use Yii;
use common\models\meeting\Meeting;
use common\models\meeting\MeetingSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use zhongkefu\zhongkefu;
use common\models\adminlog\AdminLog;
use zhongkefu\helpers\Utils;
use common\models\meeting\MeetingSign;
use common\models\meeting\MeetingSignSearch;
use common\models\member\Member;
/**
 * MeetingController implements the CRUD actions for Meeting model.
 */
class MeetingController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Meeting models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new MeetingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Meeting model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $model->newimg = json_decode($this->findModel($id)['label_img'], true)['newname'];
        $model->newfile = json_decode($this->findModel($id)['meetingdata'], true)['oldname'];
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new Meeting model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Meeting();

            //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {
			$model->starttime = Yii::$app->request->post()['Meeting']['starttime'];
			$model->endtime = Yii::$app->request->post()['Meeting']['endtime'];
			$model->created_at = time();
            $model->label_img = UploadedFile::getInstance($model, 'label_img');

            if($model->label_img){
                $siteRoot = Yii::$app->params['meetingimg'];
                $json = Utils::uploadfile($_FILES['Meeting']['tmp_name']['label_img'],$_FILES['Meeting']['name']['label_img'],$siteRoot);
                $model->label_img = $json;
            }
            
            $model->meetingdata = UploadedFile::getInstance($model, 'meetingdata');
            
            if($model->meetingdata){
                $siteRoot = Yii::$app->params['meetingfile'];
                $json = Utils::uploadfile($_FILES['Meeting']['tmp_name']['meetingdata'],$_FILES['Meeting']['name']['meetingdata'],$siteRoot);
                $model->meetingdata = $json;
            }
            if($model->save()){
                $meetingname = Yii::$app->request->post()['Meeting']['title'];
				Adminlog::addlog(yii::$app->user->id, '添加会议 '.$meetingname);
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                Yii::$app->session->setFlash('warning', Yii::t('app', '图片不能为空'));
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
          } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Meeting model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
		//var_dump(Yii::$app->request->post());die;
        //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {
            $model->label_img = UploadedFile::getInstance($model, 'label_img');
            
            if($model->label_img){
                $siteRoot = Yii::$app->params['meetingimg'];
                $json = Utils::uploadfile($_FILES['Meeting']['tmp_name']['label_img'],$_FILES['Meeting']['name']['label_img'],$siteRoot);

                $model->label_img = $json;
                
            }
			 if (!$model->label_img) {
                $new = $this->findModel($id);
                $model->label_img = $new->label_img;
            }
            
            $model->meetingdata = UploadedFile::getInstance($model, 'meetingdata');
            
            if($model->meetingdata){
                $siteRoot = Yii::$app->params['meetingfile'];
                $json = Utils::uploadfile($_FILES['Meeting']['tmp_name']['meetingdata'],$_FILES['Meeting']['name']['meetingdata'],$siteRoot);
                $model->meetingdata = $json;
            }
            if (!$model->meetingdata) {
                $new = $this->findModel($id);
                $model->meetingdata = $new->meetingdata;
            }
            $model->enroll = Yii::$app->request->post()['Meeting']['enroll'];
            if($model->save()){
				$meetingname = Yii::$app->request->post()['Meeting']['title'];
				Adminlog::addlog(yii::$app->user->id, '修改会议 '.$meetingname);
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Meeting model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }
    
    /**
     * 获取签到列表
     */
    public function actionSign($id)
    {
        $searchModel = new MeetingSignSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$id);
        
        return $this->render('sign', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
	
	
    public function actionViewsign($id)
    {
        $model = $this->findsignModel($id);
        return $this->render('viewsign', [
            'model' => $model,
        ]);
    }

    
    /**
     * Finds the Meeting model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Meeting the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Meeting::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
	
	/**
	* 查找签到model
	* 
	*/
	protected function findsignModel($id)
    {
        if (($model = Member::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

	    /**
     * 启用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStart()
    {
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = Meeting::findOne(['id' => $id]);
                Meeting::updateAll(['enroll'=>Meeting::IS_OPEN],['id' =>$model->id]);
            }
            return true;
        }
    }

    /**
     * 停用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStop()
    {
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = Meeting::findOne(['id' => $id]);
                Meeting::updateAll(['enroll'=>Meeting::NO_OPEN],['id' =>$model->id]);
            }
            return true;
        }
    }

    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
        $artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $val){
                $model = Ad::findOne(['ad_id' => $val]);
                $artname .= $model->ad_name.",";
            }
            $artname = substr($artname,0,-1);
            foreach($ids as $ad_id){
                $model = Ad::findOne(['ad_id' => $ad_id]);
                $this->findModel($ad_id)->delete();
            }
            Adminlog::addlog(yii::$app->user->id, "批量删除广告 ".$artname);
            return true;
        }
    }
    
}
